package com.example.ioc.proxy;

import com.example.ioc.anotation.JoinPoint;
import com.example.ioc.constant.CommonConstant;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class JdkProxy implements InvocationHandler {

    private Object targetObj;

    public JdkProxy(Object targetObj) {
        this.targetObj = targetObj;
    }

    private String joinPoint= CommonConstant.JOIN_POINT;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        String name = method.getName();
        JoinPoint annotation = method.getAnnotation(JoinPoint.class);//注解切点

        if (annotation!=null){
            System.out.println("前置增强");
            if (args!=null){
                for (int i = 0; i < args.length; i++) {
                    args[i]=(int)args[i]*2;
                    System.out.println(args[i]);
                }
            }
        }
        /*if (joinPoint.equals(name)){
            System.out.println("前置增强");
            if (args!=null){
                for (int i = 0; i < args.length; i++) {
                    args[i]=(int)args[i]*2;
                    System.out.println(args[i]);
                }
            }
        }*/

        Object invoke = method.invoke(targetObj, args);
        return invoke;
    }
}
